package com.controller;

import com.bean.OrderDetail;
import com.common.Result;
import com.service.OrderDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/order-details")
public class OrderDetailController {
    @Autowired
    private OrderDetailService orderDetailService;

    /**
     * 根据订单详情 ID 获取订单详情信息
     * @param detailId 订单详情的唯一标识
     * @return 封装了订单详情信息的结果对象
     */
    @GetMapping("/{detailId}")
    public Result<OrderDetail> getOrderDetailById(@PathVariable Integer detailId) {
        OrderDetail orderDetail = orderDetailService.getOrderDetailById(detailId);
        if (orderDetail != null) {
            return Result.success(orderDetail);
        } else {
            return Result.fail("订单详情不存在");
        }
    }

    /**
     * 根据订单 ID 获取该订单下的所有订单详情信息
     * @param orderId 订单的唯一标识
     * @return 封装了订单详情列表的结果对象
     */
    @GetMapping("/order/{orderId}")
    public Result<List<OrderDetail>> getOrderDetailsByOrderId(@PathVariable Integer orderId) {
        List<OrderDetail> orderDetails = orderDetailService.getOrderDetailsByOrderId(orderId);
        return Result.success(orderDetails);
    }

    /**
     * 插入新的订单详情信息
     * @param orderDetail 订单详情对象
     * @return 封装了插入结果的结果对象
     */
    @PostMapping
    public Result<Boolean> insertOrderDetail(@RequestBody OrderDetail orderDetail) {
        boolean result = orderDetailService.insertOrderDetail(orderDetail);
        if (result) {
            return Result.success(true);
        } else {
            return Result.fail("插入订单详情失败");
        }
    }

    /**
     * 根据订单详情 ID 更新订单详情信息
     * @param orderDetail 订单详情对象
     * @return 封装了更新结果的结果对象
     */
    @PutMapping
    public Result<Boolean> updateOrderDetail(@RequestBody OrderDetail orderDetail) {
        boolean result = orderDetailService.updateOrderDetail(orderDetail);
        if (result) {
            return Result.success(true);
        } else {
            return Result.fail("更新订单详情失败，订单详情可能不存在");
        }
    }

    /**
     * 根据订单详情 ID 删除订单详情信息
     * @param detailId 订单详情的唯一标识
     * @return 封装了删除结果的结果对象
     */
    @DeleteMapping("/{detailId}")
    public Result<Boolean> deleteOrderDetailById(@PathVariable Integer detailId) {
        boolean result = orderDetailService.deleteOrderDetailById(detailId);
        if (result) {
            return Result.success(true);
        } else {
            return Result.fail("删除订单详情失败，订单详情可能不存在");
        }
    }
}